Skip to content

fix(networkx): accept a callable relabel_nodes mapping - #16552

Merged
srittau merged 1 commit into
python:mainfrom
Cherith1222:fix/16545-relabel-callable
Oct 17, 2026
Merged

srittau merged 1 commit into
python:mainfrom
Cherith1222:fix/16545-relabel-callable

Conversation

@Cherith1222

Copy link
Copy Markdown
Contributor

Fixes #16545

Why

NetworkX calls a function mapping once for every node. The stub only allowed a mapping, so that documented call did not type-check.

What changed

relabel_nodes accepts a mapping or a function from the old node to the new node.

Scope

This covers the four graph overloads. The return type stays the same. Other open networkx stub pull requests are left alone.

Verification

mypy 2.4.0 rejected Callable[[int], int] and a lambda before this change, and accepts both after it. An int is still rejected. NetworkX 3.6.1 runs the documented lambda and returns [0, 1, 4].

Upstream: https://github.com/networkx/networkx/blob/networkx-3.6.1/networkx/relabel.py#L67-L122

Cursor

Drafted with Cursor.

NetworkX calls a function mapping for every node. The stub only allowed Mapping, so that documented call did not type-check.
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@srittau srittau left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@srittau
srittau merged commit b017eb6 into python:main Oct 17, 2026
74 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

nx.relabel_nodes should accept a function mapping

2 participants